package ch.ethz.fcl.metrobuzz.data.scene.basemap;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ch.ethz.fcl.metrobuzz.data.scene.spacetime.Trip.TripSegment;
import ch.ethz.fcl.metrobuzz.spatialindex.object2d.Point2D;

public class Node {
	protected String id;
	protected float x;
	protected float y;

	private List<Edge> inEdgeList = null;
	private List<Edge> outEdgeList = null;

	private Map<TripSegment, Float> tripPassTimeMap; // table of trip segment

	public Node(String id, float x, float y) {
		this.id = id;
		this.x = x;
		this.y = y;

		inEdgeList = new ArrayList<Edge>();
		outEdgeList = new ArrayList<Edge>();
	}

	public void setX(float x) {
		this.x = x;
	}

	public void setY(float y) {
		this.y = y;
	}

	public float distance(Node node) {
		return (float) Math.sqrt((x - node.x) * (x - node.x) + (y - node.y)
				* (y - node.y));
	}

	public void addTrajPassTime(TripSegment ts, float passTime) {
		if (tripPassTimeMap == null)
			tripPassTimeMap = new HashMap<TripSegment, Float>();

		if (!tripPassTimeMap.containsKey(ts))
			tripPassTimeMap.put(ts, passTime);
	}

	@Override
	public String toString() {
		return getId();
	}

	@Override
	public boolean equals(Object o) {
		if (!(o instanceof Node))
			return false;

		Node n = (Node) o;
		if (id.equals(n.id))
			return true;

		return false;
	}

	@Override
	public int hashCode() {
		return id.hashCode();
	}

	public void addIn(Edge in) {
		if (!inEdgeList.contains(in))
			inEdgeList.add(in);
	}

	public void addOut(Edge out) {
		if (!outEdgeList.contains(out))
			outEdgeList.add(out);
	}

	public int getDegree() {
		return inEdgeList.size() + outEdgeList.size();
	}

	public List<Edge> getInEdges() {
		return inEdgeList;
	}

	public List<Edge> getOutEdges() {
		return outEdgeList;
	}

	public Point2D toPoint() {
		return new Point2D(x, y);
	}

	public float getX() {
		return x;
	}

	public float getY() {
		return y;
	}

	public String getId() {
		return id;
	}

	public Map<TripSegment, Float> getTrajPassTimeMap() {
		return tripPassTimeMap;
	}
}
